package com.itlong.cloud.rabbitmq.consumer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.itlong.cloud.POJO.DTO.operate.ProjectSmartCommunityStatusChangeDTO;
import com.itlong.cloud.POJO.PO.TimeTaskDeadMessageQueuePO;
import com.itlong.cloud.constants.RabbitMQConstant;
import com.itlong.cloud.property.service.IPropertyHouseHoldInfoService;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * <desc>
 *      项目启用/禁用云对讲时，通知用户服务更新住户项目信息中的SIP
 * </desc>
 *
 * @createDate 2017-11-29
 */
@Component
public class UpdateHouseHoldProjectSipConsumer {
    private static final Logger LOG = LoggerFactory.getLogger(UpdateHouseHoldProjectSipConsumer.class);

    @Autowired
    IPropertyHouseHoldInfoService iPropertyHouseHoldInfoService;

    /**
     * <desc>
     *      项目启用/禁用云对讲时，通知用户服务更新住户项目信息中的SIP业务操作队列消费者
     * </desc>
     * @param message 消息
     * @param channel
     * @return
     * @author zhangs
     * @createDate 2017/11/29
     */
    @RabbitListener(queues = RabbitMQConstant.RABBITMQ_PROJECT_SMART_COMMUNITY_STATUS_QUEUE)
    public void process(Message message, Channel channel) throws InterruptedException, IOException {
        byte[] body = message.getBody();
        //LOG.info(String.format("【RabbitMQ_项目启用/禁用云对讲时，通知用户服务更新住户项目信息中的SIP业务消费端】", body));
        try {
            TimeTaskDeadMessageQueuePO timeTaskDeadMessageQueuePO = JSON.parseObject(new String(body), TimeTaskDeadMessageQueuePO.class);
            JSONObject dataJs = JSON.parseObject(timeTaskDeadMessageQueuePO.getMessageContent());
            JSONObject dataContent = dataJs.getJSONObject("data");
            ProjectSmartCommunityStatusChangeDTO projectSmartCommunityStatusChangeDTO = new ProjectSmartCommunityStatusChangeDTO();
            projectSmartCommunityStatusChangeDTO.setUserId(dataContent.getString("userId"));
            projectSmartCommunityStatusChangeDTO.setProjectId(dataContent.getString("projectId"));
            projectSmartCommunityStatusChangeDTO.setIsSmartCommunity(dataContent.getInteger("isSmartCommunity"));
            if (projectSmartCommunityStatusChangeDTO != null) {
                iPropertyHouseHoldInfoService.updateUserProjectSip(projectSmartCommunityStatusChangeDTO);
            }
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
            LOG.error("【RabbitMQ_项目启用/禁用云对讲时，通知用户服务更新住户项目信息中的SIP业务消费端】:解析异常",e);
        } finally {
            //手动确认消息消费成功。
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }
    }

}
